package com.dimong.www.loan.widget;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.dimong.loan.sdk.utils.Tool;
import com.dimong.www.loan.R;

public class LoanRefreshView extends FrameLayout implements View.OnClickListener {
    public static final int TYPE_FAIL = 1;
    private Context mContext;

    private View mContentView;
    private View ll_li_refresh;
    private ImageView iv_li_refresh;
    private TextView tv_li_message;

    private LoanReloadClick mLoanReloadClick;

    private int mCurrentType;

    public LoanRefreshView(Context context) {
        super(context);
        this.mContext = context;
        initView();
        setViewStatus();
    }

    public LoanRefreshView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initView();
        setViewStatus();
    }

    private void initView() {
        mContentView = View.inflate(mContext, R.layout.view_li_refresh, null);
        iv_li_refresh = mContentView.findViewById(R.id.iv_li_refresh);
        ll_li_refresh = mContentView.findViewById(R.id.ll_li_refresh);
        tv_li_message = mContentView.findViewById(R.id.tv_li_message);
        ViewGroup.LayoutParams llp = new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT);
        addView(mContentView, llp);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return true;
    }

    private void setViewStatus() {
        iv_li_refresh.setImageResource(R.mipmap.ic_launcher);
        ll_li_refresh.setOnClickListener(this);
        this.setVisibility(GONE);
    }

    /**
     * 开始刷新
     */
    public void startRefresh(boolean isHasBg) {
        this.setVisibility(VISIBLE);
        if (isHasBg) {
            ll_li_refresh.setBackgroundColor(Color.parseColor("#60000000"));
            tv_li_message.setTextColor(Tool.getColorById(R.color.white));
        } else {
            tv_li_message.setTextColor(Color.parseColor("#666666"));
            ll_li_refresh.setBackgroundColor(Tool.getColorById(R.color.transparent));
        }
        iv_li_refresh.setImageResource(R.mipmap.ic_launcher);
        tv_li_message.setText("正在加载中...");
    }

    /**
     * 结束刷新
     */
    public void stopRefresh() {
        this.setVisibility(GONE);
    }

    /**
     * 没有数据
     */
    public void showNoData(int msgId) {
        showNoData(Tool.getStringById(msgId));
    }

    /**
     * 无数据
     */
    public void showNoData(String msg) {
        this.setVisibility(VISIBLE);
        tv_li_message.setText(msg);
    }

    /**
     * 没有数据
     */
    public void showNoNet(int msgId) {
        showNoNet(Tool.getStringById(msgId));
    }

    /**
     * 无数据
     */
    public void showNoNet(String msg) {
        mCurrentType = TYPE_FAIL;
        this.setVisibility(VISIBLE);
        tv_li_message.setText(msg);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_li_refresh:
                if (mCurrentType == TYPE_FAIL && mLoanReloadClick != null) {
                    mLoanReloadClick.onRefreshClick();
                }
                break;
        }
    }


    public interface LoanReloadClick {
        void onRefreshClick();
    }

    public void setLiReloadClick(LoanReloadClick mLoanReloadClick) {
        this.mLoanReloadClick = mLoanReloadClick;
    }
}
